package org.caowei.samples.web;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.caowei.samples.dao.ISubjectDAO;
import org.caowei.samples.dao.IUserSubjectDAO;
import org.caowei.samples.model.ExamTest;
import org.caowei.samples.model.ExamTestType;
import org.caowei.samples.model.ExamTestTypeVO;
import org.caowei.samples.model.ExamTestVO;
import org.caowei.samples.model.Subject;
import org.caowei.samples.sevice.ExamTestService;
import org.caowei.samples.vo.CommonMessageResultVO;
import org.caowei.samples.vo.SubjectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;




@RequestMapping(value="/teacher")
@Controller
public class TecherController {
	
	@Autowired
	private ExamTestService examTestService;
	@Autowired
	private IUserSubjectDAO examtesttype;
	@Autowired
	private ISubjectDAO subjectdao;
	
	@ModelAttribute("Teacher")
	public ExamTest getSubject() {
		return new ExamTest();
	}
	
	@RequestMapping("/tests")
	public String users(Model model) {
		try {
			List<ExamTest> list = examTestService.findAllExamTests();
			model.addAttribute("ExamTest", list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "teacher/manager";
	}
	
	@ResponseBody
	@RequestMapping(value= "/getTestById",method=RequestMethod.POST)
	public ExamTestVO getTestById(Integer id){
		ExamTestVO resultVO=examTestService.getExamTestById(id);
		
		return resultVO;
	}
	
	@ResponseBody
	@RequestMapping(value = "/getSubjectAndTypes")
	public Map<String,Object> getSubjectAndTypes() throws Exception{
		Map<String,Object> resultMap = new HashMap<String,Object>();
		List<ExamTestType> testtypeList = examtesttype.findallitem();
		List<Subject> subjectList = subjectdao.findAllSubjects();
		
		List<SubjectVO> subjectvo=new ArrayList<SubjectVO>();
		List<ExamTestTypeVO> examtesttypevo=new ArrayList<ExamTestTypeVO>();
		
		for(Subject s:subjectList){
			SubjectVO vo=new SubjectVO();
			vo.setSubId(s.getSubId());
			vo.setSubName(s.getSubName());
			subjectvo.add(vo);	
		}
		
		for(ExamTestType e:testtypeList){	
			ExamTestTypeVO voo=new ExamTestTypeVO();
			voo.setTestType(e.getTestType());
			voo.setId(e.getId());
			voo.setTestTypeScore(e.getTestTypeScore());
			examtesttypevo.add(voo);
		}
		resultMap.put("subjectList",subjectvo);
		resultMap.put("testtypeList",examtesttypevo);
		return resultMap;
	}
	
	
	//新增
	@ResponseBody
	@RequestMapping(value = "/saveTest",method = RequestMethod.POST)
	public CommonMessageResultVO saveTest(@Valid ExamTest examTestVO,BindingResult bindingResult){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String,String> messageMap = new HashMap<String,String>();
		resultVO.setSuccess(false);
		if(bindingResult.hasErrors()){
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for(ObjectError error : errorList){
			FieldError fe = (FieldError) error;
			messageMap.put(fe.getField(), fe.getDefaultMessage());
		}
		resultVO.setMessageMap(messageMap);
		return resultVO;
	}
	try{
		examTestService.createExamTest(examTestVO);
	  resultVO.setSuccess(true);
	} catch(Exception e){
	e.printStackTrace();
	messageMap.put("message", e.getMessage());
	resultVO.setMessageMap(messageMap);
  }
    return resultVO;
	}
	
	
	//修改
	@ResponseBody
	@RequestMapping(value = "/editTest",method = RequestMethod.POST)
	public CommonMessageResultVO updateTest(@Valid ExamTestVO examTestVO,BindingResult bindingResult){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String,String> messageMap = new HashMap<String,String>();
		resultVO.setSuccess(false);
		if(bindingResult.hasErrors()){
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for(ObjectError error : errorList){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
		try{
			
			examTestService.editExamTest(examTestVO);;
		resultVO.setSuccess(true);
		} catch(Exception e){
		e.printStackTrace();
		messageMap.put("message", e.getMessage());
		resultVO.setMessageMap(messageMap);
	}
	return resultVO;
}
	
	
	/*删除*/
	@ResponseBody
	@RequestMapping(value = "/deleteTest",method = RequestMethod.POST)
	public CommonMessageResultVO deleteTest(Integer id){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String,String> messageMap = new HashMap<String,String>();
		resultVO.setSuccess(false);
		try{
			examTestService.deleteExamTest(id);
			resultVO.setSuccess(true);
			
		}catch(Exception e){
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
}
